package hedron.editor;

import static hedron.i18n.Internationalization._;
import hedron.Activator;
import hedron.editorinput.DummyEditorInput;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.EditorPart;

public class HTMLPrintEditor extends EditorPart {
	public static final String ID = "HEDRON.HTMLPrintEditor";

	private FormToolkit toolkit;
	private ScrolledForm form;

	Browser browser;

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		setSite(site);
		setInput(input);
		setTitleImage(Activator.getImage("icons/print.png"));
	}

	@Override
	public void createPartControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);

		form.setText(_("Editor.Print.Title"));
		form.setBackgroundImage(Activator.getImage("icons/form_background.png"));
		Composite body = form.getBody();
		{
			GridLayout layout = new GridLayout();
			layout.numColumns = 1;
			body.setLayout(layout);
		}
		body.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

		{
			try {
				browser = new Browser(body, SWT.NONE);
				{
					GridData gdata = new GridData(GridData.FILL, GridData.FILL, true, true);
					browser.setLayoutData(gdata);
				}

				IEditorInput editorInput = getEditorInput();
				if (editorInput instanceof DummyEditorInput) {
					Object data = ((DummyEditorInput) editorInput).getData();
					if (data != null) {
						browser.setText(data.toString());
					} else {
						browser.setText("");
					}
				} else {
					browser.setText("");
				}

			} catch (SWTError e) {
				Label label = toolkit.createLabel(body, _("Editor.Print.Warining"));
				GridData gdata = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
				label.setLayoutData(gdata);
				browser = null;
			}
		}

		{
			Action action = new Action("print", Action.AS_PUSH_BUTTON) { //$NON-NLS-1$
				public void run() {
					if (browser != null) {
						browser.execute("window.print();");
						browser.setFocus();
					}
				}
			};
			action.setToolTipText(_("Editor.Print.Tooltip.Print"));
			action.setImageDescriptor(Activator.getImageDescriptor("icons/print.png"));
			form.getToolBarManager().add(action);
		}
		form.updateToolBar();
	}

	public void dispose() {
		toolkit.dispose();
		super.dispose();
	}

	@Override
	public void setFocus() {
		if (browser != null) {
			browser.setFocus();
		}
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void doSave(IProgressMonitor monitor) {}

	@Override
	public void doSaveAs() {}

}
